package cn.edu.nyist.integration.controller;

import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.Category;
import cn.edu.nyist.integration.entity.IntegralItem;
import cn.edu.nyist.integration.entity.Integration;
import cn.edu.nyist.integration.entity.User;
import cn.edu.nyist.integration.service.IntegrationService;
import cn.edu.nyist.integration.service.ProductService;
import cn.edu.nyist.integration.vo.UserCreditVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 积分的控制层
 */
@Controller
@RequestMapping("/integration")
public class IntegrationController {
    @Autowired
    private IntegrationService integrationService;
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/my_score",method = RequestMethod.GET)
    public String queryById(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        if (user == null){
            return "redirect:/user/";
        }
        String id = user.getId();
        Long userId = Long.parseLong(id);
        //查用户信息
        ServerResponse<Integration> response = integrationService.selectById(userId);
        //查排名
        ServerResponse<UserCreditVO> scoreRank = integrationService.queryScoreRank(userId);
        UserCreditVO data = scoreRank.getData();
        model.addAttribute("userCredit",data);
        model.addAttribute("myScoreResponse",response);

        return "page/user_score";
    }

    @RequestMapping("/list")
    public String queryRank(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "8") int pageSize,
                            @RequestParam(value = "username",required = false) String username,
                            Model model){

        PageInfo pageInfo = integrationService.selectRank(pageNum,pageSize,username);

        model.addAttribute("pageInfo",pageInfo);
        return "page/integration-manager";
    }


    @RequestMapping("/addPage")
    public String toAddPage(@RequestParam String username,Model model){

        model.addAttribute("username",username);
        List<Category> categoryList = productService.listAllCategory();
        model.addAttribute("categoryList",categoryList);
        return "page/integration-add";
    }



    @GetMapping("/get_productName")
    @ResponseBody
    public ServerResponse getProductName(@RequestParam Integer categoryId){

        return productService.getProductNameByCategoryId(categoryId);
    }

    @RequestMapping("/getThree")
    @ResponseBody
    public ServerResponse<Map<String,Map>> getInsertData(){
        return productService.getInsertData();
    }

    /**
     * 根据名称获取score
     */
    @RequestMapping("/get_productScore")
    @ResponseBody
    public ServerResponse getScoreByName(@RequestParam String productName){
        return productService.getScoreByName(productName);
    }

    /**
     * 添加积分记录
     */

    @PostMapping("/addRecord")
    public String addRecord(IntegralItem integralItem, RedirectAttributes attributes){

        ServerResponse response = integrationService.updateTotalAndInsertRecord(integralItem);
        if (!response.isSuccess()){
            //如果中间有操作失败，显示错误信息
            attributes.addFlashAttribute("errorMsg",response.getMsg());
            return "redirect:/integration/addPage";
        }
        return "redirect:/integration/list";
    }

    @GetMapping("/getDetail")
    public String getDetail(@RequestParam(defaultValue = "1",value = "pageNum",required = false) int pageNum,
                            @RequestParam(defaultValue = "5",value = "pageSize",required = false) int pageSize,
            @RequestParam(value = "username") String username,Model model){
        if (username == null){
            model.addAttribute("errorMsg","获取错误，请再试一次");
        }
        PageInfo pageInfo = integrationService.getDetailList(pageNum,pageSize , username);
        model.addAttribute("pageInfo",pageInfo);
        return "page/integration-detail";
    }

}
